package com.example.demo.controller.admincontroller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.common.ErrorCode;
import com.example.demo.entity.Article;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/admin/article")
public class ArticleManageController {

    @Autowired
    private ArticleService articleService;

    /**
     * 获取文章列表
     */
    @GetMapping("/list")
    public Result getList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) Integer status
    ) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加查询条件
        if (category != null && !category.isEmpty()) {
            queryWrapper.eq(Article::getCategory, category);
        }
        if (status != null) {
            queryWrapper.eq(Article::getStatus, status);
        }
        
        queryWrapper.orderByAsc(Article::getId);
        
        // 分页查询
        Page<Article> pageResult = articleService.page(new Page<>(page, size), queryWrapper);
        return Result.success(pageResult);
    }

    /**
     * 获取文章详情
     */
    @GetMapping("/{id}")
    public Result getDetail(@PathVariable Long id) {
        Article article = articleService.getById(id);
        if (article == null) {
            return Result.error(ErrorCode.NOT_FOUND_ERROR);
        }
        return Result.success(article);
    }

    /**
     * 创建文章
     */
    @PostMapping("/create")
    public Result create(@RequestBody Article article) {
        // 设置初始值
        article.setViews(0);
        article.setCreateTime(new Date());
        article.setUpdateTime(new Date());
        
        if (articleService.save(article)) {
            return Result.success("创建成功");
        }
        return Result.error(ErrorCode.OPERATION_ERROR);
    }

    /**
     * 更新文章
     */
    @PutMapping("/update/{id}")
    public Result update(@PathVariable Long id, @RequestBody Article article) {
        Article existingArticle = articleService.getById(id);
        if (existingArticle == null) {
            return Result.error(ErrorCode.NOT_FOUND_ERROR);
        }
        
        article.setId(id);
        article.setUpdateTime(new Date());
        
        if (articleService.updateById(article)) {
            return Result.success("更新成功");
        }
        return Result.error(ErrorCode.OPERATION_ERROR);
    }

    /**
     * 删除文章
     */
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        if (articleService.removeById(id)) {
            return Result.success("删除成功");
        }
        return Result.error(ErrorCode.OPERATION_ERROR);
    }

    /**
     * 更新文章状态（发布/下架）
     */
    @PutMapping("/status/{id}")
    public Result updateStatus(
            @PathVariable Long id,
            @RequestParam Integer status
    ) {
        Article article = new Article();
        article.setId(id);
        article.setStatus(status);
        article.setUpdateTime(new Date());
        
        if (articleService.updateById(article)) {
            return Result.success("状态更新成功");
        }
        return Result.error(ErrorCode.OPERATION_ERROR);
    }
} 